package org.pine.api.beans.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页响应体
 *
 * @author pine
 * @version 1.0
 * @since 2025-04-10 17:40
 */
@Data
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "分页响应体")
public class PageResponse<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -7670384746939199040L;

    @Schema(title = "分页数据", description = "分页数据")
    private List<T> list;

    @Schema(title = "总条数", description = "总条数")
    private long total;

    @Schema(title = "总页数", description = "总页数")
    private long pages;

    @Schema(title = "分页大小", description = "分页大小")
    private long pageSize;

    @Schema(title = "分页页码", description = "分页页码")
    private long pageNum;

    @SuppressWarnings("unused")
    public PageResponse() {
        this.list = new ArrayList<>(0);
        this.total = 0;
        this.pages = 0;
        this.pageNum = 1;
        this.pageSize = 0;
    }

    public PageResponse(List<T> data, Long total, Long page, Long pageSize) {
        this.list = (null == data ? new ArrayList<T>(0) : data);
        this.total = total;
        this.pageNum = page;
        this.pageSize = pageSize;
        this.pages = (int) Math.ceil((double) total / (double) pageSize);
    }
}
